#include "texture.hpp"
#include "renderer.hpp"
#include "stb_image/stb_image.h"
#include <iostream>

Texture::Texture( std::string const& filePath )
    : _filePath( filePath ),
    _rendererID( 0 ),
    _localBuffer( nullptr ),
    _width( 0 ),
    _height( 0 ),
    _BPP( 0 ),
    _solt( -1 )
{
    stbi_set_flip_vertically_on_load( 1 );
    _localBuffer = stbi_load( filePath.c_str(), &_width, &_height, &_BPP, 4 );

    GLCall( glGenTextures( 1, &_rendererID ) );
    GLCall( glBindTexture( GL_TEXTURE_2D, _rendererID ) );

    GLCall( glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR ) );
    GLCall( glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR ) );
    GLCall( glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE ) );
    GLCall( glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE ) );

    GLCall( glTexImage2D( GL_TEXTURE_2D, 0, GL_RGBA8, _width, _height, 0, GL_RGBA, GL_UNSIGNED_BYTE, _localBuffer ) );
    GLCall( glBindTexture( GL_TEXTURE_2D, _rendererID ) );

    if ( _localBuffer ) {
        stbi_image_free( _localBuffer );
        _localBuffer = nullptr;
    }
    else {
        std::cout << "can't load texture: " << filePath << std::endl;
    }
}
Texture::~Texture() {
    GLCall( glDeleteTextures( 1, &_rendererID ) );
}

void Texture::bind( unsigned int solt ) {
    _solt = 0 + solt;
    GLCall( glActiveTexture( GL_TEXTURE0 + solt ) );
    GLCall( glBindTexture( GL_TEXTURE_2D, _rendererID ) );
}
void Texture::unbind() {
    GLCall( glBindTexture( GL_TEXTURE_2D, 0 ) );
    _solt = 0;
}
